$(function(){    
    var tip = function(obj, info) {
        obj.tip({
            content: info,
            background: "#000",
            color: "#fff"
        });
    };
    var receptionLabel = $('#receptionLabel');
    var receptionSwitch = $('#receptionSwitch');
    var identificationLabel = $('#identificationLabel');
    var identificationSwitch = $('#identificationSwitch');
    var livemateShareLabel = $('#livemateShareLabel');
    var livemateShareSwitch = $('#livemateShareSwitch');
    var isOpenIdentificationLabel = function(open){
        if(open){
            identificationSwitch.closest('.content-panel-header').addClass('disabled');
        }else{
            identificationSwitch.closest('.content-panel-header').removeClass('disabled');
        }        
    };
    ajaxRequest('/Home/Hotel/toggleCheckinInfo', {
        "hotel": window.currentHotel,
    }, 'post', function(data) {
        var checkinUsable = data.data.checkinUsable;   
        var hotelCheckinEnable = checkinUsable.hotelCheckinEnable;
        var hotelIdCheckinEnable = checkinUsable.hotelIdCheckinEnable;
        if(hotelCheckinEnable){
            receptionLabel.text(lang('yi'));
            receptionSwitch.data('value',1).addClass('is-checked');
        }else{
            receptionLabel.text(lang('wei'));
            receptionSwitch.data('value',0).removeClass('is-checked');
        };
        if(hotelIdCheckinEnable){
            identificationLabel.text(lang('yi'));
            identificationSwitch.data('value',1).addClass('is-checked');
        }else{
            identificationLabel.text(lang('wei'));
            identificationSwitch.data('value',0).removeClass('is-checked');
        };
        isOpenIdentificationLabel(hotelCheckinEnable);
    }, function(error) { 
        
    });    
    
    receptionSwitch.on('click',function(){
        var submitBtn = $(this);
        var value = submitBtn.data('value');
        var nextValue = identificationSwitch.data('value');
        var type = 0;
        if(value == 0 && nextValue == 1){
            type = 3;
        }else if(value == 0){
            type = 2;
        }else if(nextValue == 1){
            type = 1;
        } 
        ajaxRequest('/Home/Hotel/toggleCheckinType', {
            "hotel": window.currentHotel,
            'type' : type
        }, 'post', function(data) {
            if(value == 1){
                submitBtn.data('value',0).removeClass('is-checked');
                receptionLabel.text(lang('wei'));
                isOpenIdentificationLabel(false);
            }else{
                submitBtn.data('value',1).addClass('is-checked');
                receptionLabel.text(lang('yi'));
                isOpenIdentificationLabel(true);
            }              
        }, function(error) { 
            tip(submitBtn, error.responseJSON.info);
        });
    });
    identificationSwitch.on('click',function(){
        var submitBtn = $(this);
        var value = submitBtn.data('value');
        var nextValue = receptionSwitch.data('value');
        var type = 0;
        if(value == 0 && nextValue == 1){
            type = 3;
        }else if(value == 0){
            type = 1;
        }else if(nextValue == 1){
            type = 2;
        } 
        ajaxRequest('/Home/Hotel/toggleCheckinType', {
            "hotel": window.currentHotel,
            'type' : type
        }, 'post', function(data) {
            if(value == 1){
                submitBtn.data('value',0).removeClass('is-checked');
                identificationLabel.text(lang('wei'));
            }else{
                submitBtn.data('value',1).addClass('is-checked');
                identificationLabel.text(lang('yi'));
            }                 
        }, function(error) { 
            tip(submitBtn, error.responseJSON.info);
        });            
    });    
    livemateShareSwitch.on('click',function(){
        var submitBtn = $(this);
        if(submitBtn.hasClass('is-checked')){
            var status  = 0;
        }else{
            var status  = 1;
        }              
        ajaxRequest('/Home/BookSetting/toggleBookShareKey', {
            "hotel": window.currentHotel,
            'status' : status 
        }, 'post', function(data) {
            if(status  == 1){
                submitBtn.addClass('is-checked');
                $('#livemateShareLabel').text(lang('yi'));
            }else{
                submitBtn.removeClass('is-checked');
                $('#livemateShareLabel').text(lang('wei'));
            }                 
        }, function(error) { 
            tip(submitBtn, error.responseJSON.info);
        });         
    });   
});
